package composites

import (
	b3 "gitee.com/zhenghexcs/bevtreestatus"
	. "gitee.com/zhenghexcs/bevtreestatus/core"
)

type MemPriority struct {
	Composite
}

/**
 * Open method.
 * @method open
 * @param {b3.Tick} tick A tick instance.
**/
func (this *MemPriority) OnOpen(tick *Tick) {
	tick.Blackboard.SetMem(this.RunningChildKey, 0)
}

/**
 * Tick method.
 * @method tick
 * @param {b3.Tick} tick A tick instance.
 * @return {Constant} A state constant.
**/
func (this *MemPriority) OnTick(tick *Tick) b3.Status {
	var child = tick.Blackboard.GetInt(this.RunningChildKey)
	for i := child; i < this.GetChildCount(); i++ {
		var status = this.GetChild(i).Execute(tick)

		if status != b3.FAILURE {
			if status == b3.RUNNING {
				tick.Blackboard.SetMem(this.RunningChildKey, i)
			}

			return status
		}
	}
	return b3.FAILURE
}
